import 'package:pornhub_app/components/base_refresh/base_refresh_widget.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/mine/buy/controllers/buy_video_page_controller.dart';
import 'package:pornhub_app/views/mine/buy/views/video_buy_record_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class BuyVideoPage extends GetView<BuyVideoPageController> {
  const BuyVideoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      return Scaffold(
        appBar: _buildAppBar(context),
        body: Column(
          children: <Widget>[
            Expanded(
              child: BaseRefreshWidget(
                controller,
                child: _buildContentView(context),
              ),
            ),
            _buildBottomView(context),
          ],
        ),
      );
    });
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      leading: Container(
        padding: EdgeInsets.all(20.w),
        child: Image.asset(
          'assets/images/icons/base_ic_back_white.png',
          width: 9.w,
          height: 16.w,
        ),
      ).onOpaqueTap(() {
        onBackClick(context);
      }),
      title: Text('购买视频',
          textAlign: TextAlign.center,
          style: TextStyle(
            color: COLOR.hexColor("#fff"),
            fontSize: 17.sp,
            fontWeight: FontWeight.w500,
          )),
      centerTitle: true,
      actions: <Widget>[
        Container(
          padding: EdgeInsets.all(15.w),
          child: Text(
            controller.visible.value ? '完成' : '编辑',
            textAlign: TextAlign.center,
            style: TextStyle(
              color: COLOR.hexColor("#fff"),
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
        ).onOpaqueTap(() {
          controller.visible.value = !controller.visible.value;
          controller.onResetItem();
        }),
      ],
      bottom: PreferredSize(
        preferredSize: const Size.fromHeight(0.5),
        child: Container(
          height: 0.5,
          color: COLOR.hexColor('#393549'),
        ),
      ),
    );
  }

  Widget _buildContentView(BuildContext context) {
    return ListView.separated(
      physics: const AlwaysScrollableScrollPhysics(),
      itemBuilder: (_, index) {
        return VideoBuyRecordItem(
          video: controller.videos[index],
          index: index,
        ).onOpaqueTap(() {
          if (!controller.visible.value) {
            Get.toPlayVideo(videoId: controller.videos[index].videoId!);
          }
        });
      },
      separatorBuilder: (_, __) => SizedBox(height: 10.w),
      itemCount: controller.videos.length,
    ).baseMarginLtr;
  }

  Widget _buildBottomView(BuildContext context) {
    return Visibility(
        maintainSize: false,
        maintainAnimation: true,
        maintainState: true,
        maintainInteractivity: false,
        visible: controller.visible.value,
        child: Flex(
          direction: Axis.horizontal,
          children: <Widget>[
            Expanded(
              flex: 1,
              child: Row(
                children: <Widget>[
                  Image.asset(
                    controller.allSelect.value
                        ? 'assets/mine/icon_video_select.png'
                        : 'assets/mine/icon_video_unselect.png',
                    width: 22.w,
                    height: 22.w,
                  ),
                  SizedBox(width: 6.w),
                  Text(controller.allSelect.value ? '全不选' : '全选',
                      style: TextStyle(
                        color: COLOR.hexColor('#fff'),
                        fontSize: 14.sp,
                        fontWeight: FontWeight.w500,
                      )),
                ],
              ).onOpaqueTap(() {
                controller.allSelect.value = !controller.allSelect.value;
                controller.onAllSelected();
              }),
            ),
            Container(
              width: 100.w,
              height: 40.w,
              decoration: const BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage('assets/mine/icon_video_delete.png'),
                      fit: BoxFit.cover,
                      filterQuality: FilterQuality.low)),
              alignment: Alignment.center,
              child: Text('删除',
                  style: TextStyle(
                    color: COLOR.hexColor('#fff'),
                    fontSize: 13.sp,
                    fontWeight: FontWeight.w500,
                  )),
            ).onOpaqueTap(() {
              controller.onDeleteItem();
            }),
          ],
        ).baseMarginHorizontal.baseMarginVertical);
  }

  void onBackClick(BuildContext context) {
    Navigator.pop(context);
  }
}
